package com.seat.row.widget;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.seat.row.App;
import com.seat.row.R;
import com.seat.row.interfaces.OnAlertEventListener;
import com.seat.row.util.IIntent;
import com.seat.row.util.KeyConfig;
import com.seat.row.util.StringUtil;
import com.seat.row.util.UIUtil;

import java.security.Key;

public class UIAlertDialog extends Dialog {
    private View mDialogView;
    private AnimationSet mModalInAnim;
    private AnimationSet mModalOutAnim;

    private Context context;


    public UIAlertDialog(Context context) {
        this(context, POPConfig.NORMAL_TYPE);
    }

    private int mAlertType;

    public UIAlertDialog(Context context, int alertType) {
        super(context, R.style.alert_dialog);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        this.context = context;
        mAlertType = alertType;
        mModalInAnim = (AnimationSet) OptAnimationLoader.loadAnimation(getContext(), R.anim.modal_in);
        mModalOutAnim = (AnimationSet) OptAnimationLoader.loadAnimation(getContext(), R.anim.modal_out);
        mModalOutAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mDialogView.setVisibility(View.GONE);
                mDialogView.post(new Runnable() {
                    @Override
                    public void run() {
                        UIAlertDialog.super.dismiss();
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ui_alert_dialog);
        mDialogView = getWindow().getDecorView().findViewById(android.R.id.content);

        switch (mAlertType) {
            case POPConfig.NORMAL_TYPE:
                normalPop();
                break;
            case POPConfig.ENTER_IP:
                enterIp();
                break;
            default:
                break;
        }
        changeAlertType(mAlertType, true);
    }


    private LinearLayout enterIpLayout;
    private EditText ipEnter;

    public String getUrl(){
        return ipEnter.getText().toString().trim();
    }
    private void enterIp() {
        ipEnter = findViewById(R.id.ipEnter);
        enterIpLayout = findViewById(R.id.enterIpLayout);
        enterIpLayout.setMinimumWidth(UIUtil.getScreenWidth(context) / 2);
        findViewById(R.id.ipCancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        findViewById(R.id.ipSure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onAlertEventListener.clickEvent(KeyConfig.ENTER_IP_SURE);
                dismiss();
            }
        });
        String url = App.getInstance().getACache().getAsString(KeyConfig.lastUrl);
        if (!StringUtil.isEmptyOrNull(url)){
            ipEnter.setText(url);
        }
    }

    private LinearLayout pop_dialog_bo, dialogContentLL;
    private TextView dialog_sure, dialog_cancel, dialg_title, dialog_content;
    private LinearLayout titleLL;

    public void setTitle(String str) {
        dialg_title.setText(str);
    }

    private void normalPop() {
        titleLL = (LinearLayout) findViewById(R.id.titleLL);
        dialogContentLL = (LinearLayout) findViewById(R.id.dialogContentLL);
        dialg_title = (TextView) findViewById(R.id.dialog_title);
        pop_dialog_bo = (LinearLayout) findViewById(R.id.pop_dialog_bo);
        dialog_sure = (TextView) findViewById(R.id.dialog_sure);
        dialog_cancel = (TextView) findViewById(R.id.dialog_cancel);
        dialog_content = (TextView) findViewById(R.id.dialog_content);
        dialogContentLL.setVisibility(View.GONE);
        dialog_sure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                onAlertEventListener.clickEvent(1);
                dismiss();
            }
        });
        dialog_cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                onAlertEventListener.clickEvent(0);
                dismiss();
            }
        });
    }


    private void changeAlertType(int alertType, boolean fromCreate) {
        if (mDialogView != null) {
            switch (mAlertType) {
                case POPConfig.NORMAL_TYPE:
                    pop_dialog_bo.setVisibility(View.VISIBLE);
                    break;
                case POPConfig.ENTER_IP:
                    enterIpLayout.setVisibility(View.VISIBLE);
                    break;
                default:
                    break;
            }
        }
    }

    public int getAlerType() {
        return mAlertType;
    }

    protected void onStart() {
        mDialogView.startAnimation(mModalInAnim);
    }

    public void dismiss() {
        if (mDialogView != null && mModalOutAnim != null) {
            mDialogView.startAnimation(mModalOutAnim);
        }
    }


    private OnAlertEventListener onAlertEventListener;

    public void setOnAlertEventListener(OnAlertEventListener contentclick) {
        this.onAlertEventListener = contentclick;
    }

}
